package com.kexio.user.entity;

import java.util.List;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.kexio.common.entity.BaseEntity;

/**
 * 菜单实体类
 * 
 * 对应数据库表 sys_menu
 * 存储系统菜单信息，用于构建动态菜单和权限控制
 * 
 * @author Kexio Team
 * @since 1.0.0
 */
@TableName("sys_menu")
public class Menu extends BaseEntity {
    
    /**
     * 菜单ID（主键）- UUID字符串
     */
    @TableId(value = "id", type = IdType.ASSIGN_UUID)
    private String id;
    
    /**
     * 父菜单ID - UUID字符串
     */
    @TableField("parent_id")
    private String parentId;
    
    /**
     * 菜单名称
     */
    @TableField("name")
    private String name;
    
    /**
     * 菜单标题（显示用）
     */
    @TableField("title")
    private String title;
    
    /**
     * 路由路径
     */
    @TableField("path")
    private String path;
    
    /**
     * 前端组件路径
     */
    @TableField("component")
    private String component;
    
    /**
     * 菜单图标
     */
    @TableField("icon")
    private String icon;
    
    /**
     * 菜单类型：M-目录，C-菜单，F-按钮
     */
    @TableField("menu_type")
    private String menuType;
    
    /**
     * 权限标识（可选，在新架构下权限通过menu_id字段直接关联菜单）
     */
    @TableField("permission")
    private String permission;
    
    /**
     * 排序号
     */
    @TableField("order_no")
    private Integer orderNo;
    
    /**
     * 是否外链：1-是，0-否
     */
    @TableField("is_frame")
    private Boolean isFrame;
    
    /**
     * 是否缓存：1-缓存，0-不缓存
     */
    @TableField("is_cache")
    private Boolean isCache;
    
    /**
     * 是否可见：1-显示，0-隐藏
     */
    @TableField("visible")
    private Boolean visible;
    
    /**
     * 菜单状态：1-启用，0-禁用
     */
    @TableField("status")
    private Integer status;
    
    /**
     * 路由参数
     */
    @TableField("query_params")
    private String queryParams;
    
    /**
     * 备注
     */
    @TableField("remark")
    private String remark;
    
    /**
     * 菜单元数据（JSON格式，存储额外配置信息）
     */
    @TableField("metadata")
    private String metadata;
    
    /**
     * 删除标记
     * 
     * 重写父类字段以启用逻辑删除。
     * 0表示未删除，1表示已删除。
     */
    @TableField(value = "deleted", fill = FieldFill.INSERT)
    @TableLogic(value = "0", delval = "1")
    @JsonIgnore
    private Integer deleted;
    

    // ==================== 非数据库字段（用于关联查询） ====================
    
    /**
     * 子菜单列表（非数据库字段）
     */
    @TableField(exist = false)
    private List<Menu> children;
    
    /**
     * 菜单关联的权限列表（非数据库字段）
     */
    @TableField(exist = false)
    private List<Permission> permissions;
    
    /**
     * 父菜单名称（非数据库字段）
     */
    @TableField(exist = false)
    private String parentName;
    
    // ==================== 便捷方法 ====================
    
    /**
     * 判断菜单是否启用
     */
    public boolean isEnabled() {
        return status != null && status == 1;
    }
    
    /**
     * 判断菜单是否可见
     */
    public boolean checkVisible() {
        return visible != null && visible;
    }
    
    /**
     * 判断是否为根菜单
     */
    public boolean isRootMenu() {
        return parentId == null || "0".equals(parentId);
    }
    
    /**
     * 判断是否为目录
     */
    public boolean isDirectory() {
        return "DIR".equals(this.menuType);
    }
    
    /**
     * 判断是否为菜单
     */
    public boolean isMenu() {
        return "MENU".equals(this.menuType);
    }
    
    /**
     * 判断是否为按钮
     */
    public boolean isButton() {
        return "BUTTON".equals(this.menuType);
    }
    
    /**
     * 判断是否为外链
     */
    public boolean isFrame() {
        return isFrame != null && isFrame;
    }
    
    /**
     * 判断是否缓存
     */
    public boolean isCache() {
        return isCache != null && isCache;
    }
    
    /**
     * 判断是否有子菜单
     */
    public boolean hasChildren() {
        return children != null && !children.isEmpty();
    }
    
    /**
     * 添加子菜单
     */
    public void addChild(Menu child) {
        if (this.children == null) {
            this.children = new java.util.ArrayList<>();
        }
        this.children.add(child);
    }
    
    // ==================== Getter/Setter ====================
    
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }

    public String getParentId() {
        return parentId;
    }
    
    public void setParentId(String parentId) {
        this.parentId = parentId;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getTitle() {
        return title;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }
    
    public String getPath() {
        return path;
    }
    
    public void setPath(String path) {
        this.path = path;
    }
    
    public String getComponent() {
        return component;
    }
    
    public void setComponent(String component) {
        this.component = component;
    }
    
    public String getIcon() {
        return icon;
    }
    
    public void setIcon(String icon) {
        this.icon = icon;
    }
    
    public String getMenuType() {
        return menuType;
    }
    
    public void setMenuType(String menuType) {
        this.menuType = menuType;
    }
    
    public String getPermission() {
        return permission;
    }
    
    public void setPermission(String permission) {
        this.permission = permission;
    }
    
    public Integer getOrderNo() {
        return orderNo;
    }
    
    public void setOrderNo(Integer orderNo) {
        this.orderNo = orderNo;
    }
    
    public Boolean getIsFrame() {
        return isFrame;
    }
    
    public void setIsFrame(Boolean isFrame) {
        this.isFrame = isFrame;
    }
    
    public Boolean getIsCache() {
        return isCache;
    }
    
    public void setIsCache(Boolean isCache) {
        this.isCache = isCache;
    }
    
    public Boolean getVisible() {
        return visible;
    }
    
    public void setVisible(Boolean visible) {
        this.visible = visible;
    }
    
    public Integer getStatus() {
        return status;
    }
    
    public void setStatus(Integer status) {
        this.status = status;
    }
    
    public String getQueryParams() {
        return queryParams;
    }
    
    public void setQueryParams(String queryParams) {
        this.queryParams = queryParams;
    }
    
    public String getRemark() {
        return remark;
    }
    
    public void setRemark(String remark) {
        this.remark = remark;
    }
    
    public String getMetadata() {
        return metadata;
    }
    
    public void setMetadata(String metadata) {
        this.metadata = metadata;
    }
    
    
    public List<Menu> getChildren() {
        return children;
    }
    
    public void setChildren(List<Menu> children) {
        this.children = children;
    }
    
    public List<Permission> getPermissions() {
        return permissions;
    }
    
    public void setPermissions(List<Permission> permissions) {
        this.permissions = permissions;
    }
    
    public String getParentName() {
        return parentName;
    }
    
    public void setParentName(String parentName) {
        this.parentName = parentName;
    }
    
    @Override
    public String toString() {
        return "Menu{" +
                "parentId='" + parentId + '\'' +
                ", name='" + name + '\'' +
                ", title='" + title + '\'' +
                ", path='" + path + '\'' +
                ", component='" + component + '\'' +
                ", icon='" + icon + '\'' +
                ", menuType='" + menuType + '\'' +
                ", permission='" + permission + '\'' +
                ", orderNo=" + orderNo +
                ", isFrame=" + isFrame +
                ", isCache=" + isCache +
                ", visible=" + visible +
                ", status=" + status +
                ", queryParams='" + queryParams + '\'' +
                ", remark='" + remark + '\'' +
                '}';
    }
}
